package kuangshen.stop;

/**
 * @program: liangt
 * @description: 测试线程自动停止
 * @author: liangt
 * @create: 2021-03-01 16:08
 */

/**
 * 见识线程自动停止 -->利用次数停止，不建议死循环
 * 建议使用标志位停止
 * 不建议使用stop或者destory
 */
public class TestStop implements Runnable{

    private boolean flag = false;

    @Override
    public void run() {
        int i = 0;
        while (!flag){
            System.out.println("run Thread now "+ i++);
        }
    }

    public void stop(){
        flag = true;
    }

    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop,"测试线程停止").start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("main = "+i);
            if (i == 900){
                testStop.stop();
                System.out.println("线程该停止了");
            }
        }
    }
}
